package com.bluedot.bank.action;

import com.bluedot.bank.dao.Pagination;
import com.bluedot.bank.dao.customer.CustomerConditons;
import com.bluedot.bank.dao.role.Role;
import com.bluedot.bank.dao.role.RoleDao;
import com.bluedot.bank.dao.role.RoleDaoImpl;
import com.opensymphony.xwork2.ActionSupport;

public class FindRoleAction extends ActionSupport {
	private Pagination<Role> pagination=new Pagination<Role>();
	private int filterName;
	private String value_r;
	@Override
	public String execute() throws Exception {
		RoleDao roleDao=new RoleDaoImpl();
		switch (filterName) {
		case 1:
			pagesResult(pagination, null);
			roleDao.browse(pagination);
			break;
		case 2:
			pagesResult(pagination, getCondition("roleIdFilter", "roleId",
					Long.parseLong(value_r)));
			roleDao.browse(pagination);
			break;
		case 3:
			pagesResult(pagination, getCondition("roleDescriptionFilter",
					"roleDescription", "%" + value_r + "%"));
			roleDao.browse(pagination);
			break;
		
		default:
			pagesResult(pagination, null);
			roleDao.browse(pagination);
			break;
		}
		return SUCCESS;
	}

	void pagesResult(Pagination<Role> pagination,
			CustomerConditons conditons) {
		if (this.pagination == null) {
			this.pagination = new Pagination<Role>();
		}

		if (this.pagination.getSize() == 0) {
			this.pagination.setSize(3);
		}

		if (this.pagination.getNo() == 0) {
			this.pagination.setNo(1);
		}
		pagination.setConditon(conditons);
	}

	CustomerConditons getCondition(String filterName, String key, Object o) {
		CustomerConditons conditons = new CustomerConditons();
		conditons.setFilterName(filterName);
		conditons.setKey(key);
		conditons.setO(o);
		return conditons;
	}

	public Pagination<Role> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<Role> pagination) {
		this.pagination = pagination;
	}

	public int getFilterName() {
		return filterName;
	}

	public void setFilterName(int filterName) {
		this.filterName = filterName;
	}

	public String getValue_r() {
		return value_r;
	}

	public void setValue_r(String value_r) {
		this.value_r = value_r;
	}
}
